using UnityEngine;
using System.Collections;

public class ChangeParticleDemo : MonoBehaviour {

    public ParticleSystem _particle;
    private Color currentColor;

	void LateUpdate () 
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            currentColor = Color.blue;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            currentColor = Color.green;
        }

        changeColorParticle(currentColor);
	}

    private void changeColorParticle(Color newColor)
    {
        this._particle.startColor = newColor;
        ParticleSystem.Particle[] particles = new ParticleSystem.Particle[_particle.particleCount];
        _particle.GetParticles(particles);

        for (int i = 0; i < particles.Length; i++)
        {
            float lifePercentage = (particles[i].lifetime / particles[i].startLifetime);

            Color finalColor = new Color(newColor.r,newColor.g,newColor.b,0.4f);
            particles[i].color = Color.Lerp(finalColor, newColor, lifePercentage);
            particles[i].angularVelocity = 0;
            particles[i].velocity = Vector3.zero;
        }

        _particle.SetParticles(particles,particles.Length);
    }
}
